<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <RouterLink to="/">
          <div class="title-bar">
            <img class="logo" src="../../assets/logo.png" alt="logo" />
            <div class="title">云图库</div>
          </div>
        </RouterLink>
      </a-col>
      <a-col flex="auto"></a-col>
      <a-col flex="120px">
        <div v-if="loginUserStore.loginUser?.userName !== '未登录'">
          <a-dropdown>
            <div class="user-info">
              <a-avatar :src="loginUserStore.loginUser?.userAvatar" :size="32" class="user-avatar">
                {{ loginUserStore.loginUser.userName?.charAt(0).toUpperCase() }}
              </a-avatar>
              <span class="user-name">{{ loginUserStore.loginUser.userName }}</span>
            </div>
            <template #overlay>
              <a-menu>
                <a-menu-item>
                  <div class="dropdown-item" @click="handleToUserCenter">
                    <UserOutlined />
                    <span>个人中心</span>
                  </div>
                </a-menu-item>
                <a-menu-divider />
                <a-menu-item>
                  <div class="dropdown-item" @click="handleLogout">
                    <LogoutOutlined />
                    <span>退出登录</span>
                  </div>
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
        <div v-else>
          <a-button type="primary" href="/user/login">登录</a-button>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { h, ref, watchEffect } from 'vue'
import { MailOutlined, AppstoreOutlined, UserOutlined, LogoutOutlined } from '@ant-design/icons-vue'
import { MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore'
import { userLogoutUsingPost } from '@/api/userController'

const loginUserStore = useLoginUserStore()
const router = useRouter()

// 图标数组
const iconArray = [MailOutlined, AppstoreOutlined, UserOutlined, LogoutOutlined]

// 动态生成导航栏项目
const items = ref<MenuProps['items']>(
  router
    .getRoutes()
    .filter((route) => route.meta.showInNav)
    .map((route) => {
      const randomIcon = iconArray[Math.floor(Math.random() * iconArray.length)]
      return {
        key: route.path,
        icon: () => h(randomIcon), // 随机选择一个图标
        label: String(route.name), // 确保 label 是字符串
        title: String(route.name), // 确保 title 是字符串
      }
    }),
)

const current = ref<string[]>([])
const handleClick = ({ key }: { key: string }) => {
  router.push({
    path: key,
  })
}
router.afterEach((to, from, next) => {
  //初始化的时候指定url上的路径
  current.value = [to.path]
})
watchEffect(() => {
  loginUserStore.fetchLoginUser()
})

const handleLogout = async () => {
  try {
    const res = await userLogoutUsingPost()
    if (res.data.code === 0) {
      // 清除用户状态和本地存储
      loginUserStore.setLoginUser({
        userName: '未登录',
      })
      message.success('退出登录成功')
      router.push('/user/login')
    }
  } catch (error) {
    message.error('退出登录失败')
  }
}

const handleToUserCenter = () => {
  router.push('/about')
}
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 36px;
  margin-left: 10px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 0 8px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: rgba(79, 70, 229, 0.1);
  border-radius: 6px;
}

.user-avatar {
  background-color: #4f46e5;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  color: #ffffff;
}

.user-name {
  color: #1e293b;
  font-size: 14px;
}

.user-info:hover .user-name {
  color: #4f46e5;
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
}

.dropdown-item:hover {
  color: #4f46e5;
}

:deep(.ant-dropdown-menu-item) {
  padding: 8px 16px;
}

:deep(.ant-dropdown-menu) {
  min-width: 120px;
}
</style>
